package com.mocoop.plugin.page;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaArgs;
import org.json.JSONException;
import org.json.JSONObject;

import com.mocoop.entity.PageInfoEntity;
import com.mocoop.entity.PageInfoEntity.Bar;
import com.mocoop.entity.PageInfoEntity.ButtonItem;
import com.mocoop.entity.PageInfoEntity.ContentView;
import com.mocoop.entity.PageInfoEntity.MocoopStyle;
import com.mocoop.jfqc.MainActivity;
import com.mocoop.jfqc.JFQCActivity.BaseActivity;
import com.mocoop.plugin.MocoopPlugin;

import android.content.Context;

public class pagesService extends MocoopPlugin {

	public static Map<String, MocoopStyle> styleMap = new HashMap<String, MocoopStyle>();

	public static MocoopStyle getStyleObject(String path, Context context) {

		MocoopStyle mocoopStyle = styleMap.get(path);
		if (mocoopStyle != null) {
			return mocoopStyle;
		}

		try {
			InputStream is = context.getAssets().open("html/" + path);
			byte[] buffer = new byte[is.available()];
			is.read(buffer);
			String json = new String(buffer, "utf-8");
			is.close();
			JSONObject obj;
			try {

				mocoopStyle = new MocoopStyle();
				obj = new JSONObject(json);

				JSONObject topBarObj = obj.getJSONObject("topBar");
				Bar topBar = new Bar();
				ContentView contentView = new ContentView();
				ButtonItem rightBarButtonItem = new ButtonItem();
				ButtonItem leftBarButtonItem = new ButtonItem();

				topBar.setBarTintColor(topBarObj.getString("barTintColor"));
				topBar.setTitleColor(topBarObj.getString("titleColor"));
				try {
					topBar.setLeftImgBut(topBarObj.getString("backIndicator"));
				} catch (JSONException e) {
					e.printStackTrace();
				}
				try {
					JSONObject leftBarButtonItemObj = topBarObj
							.getJSONObject("leftBarButtonItem");
					leftBarButtonItem.setFontSize(leftBarButtonItemObj
							.getInt("fontSize"));
					leftBarButtonItem.setTintColor(leftBarButtonItemObj
							.getString("tintColor"));
					leftBarButtonItem.setTitle(leftBarButtonItemObj
							.getString("title"));
					topBar.setLeftButtonItem(leftBarButtonItem);
				} catch (JSONException e) {
					e.printStackTrace();
				}
				try {
					JSONObject rightBarButtonItemObj = (JSONObject) topBarObj
							.getJSONArray("rightBarButtonItem").get(0);
					rightBarButtonItem.setFontSize(rightBarButtonItemObj
							.getInt("fontSize"));
					rightBarButtonItem.setTintColor(rightBarButtonItemObj
							.getString("tintColor"));
					rightBarButtonItem.setTitle(rightBarButtonItemObj
							.getString("title"));
					rightBarButtonItem.setEventNameForTriggerOnClick("'"
							+ rightBarButtonItemObj
									.getString("eventNameForTriggerOnClick")
							+ "'");
					try{
						topBar.setRightImgBut("html/"+rightBarButtonItemObj
								.getString("iconFileSubPathWithoutExtension")+".png");
					}catch(JSONException e){
						
					}
					
					topBar.setRightButtonItem(rightBarButtonItem);
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
				
				
				try {
					JSONObject contentViewObj = obj
							.getJSONObject("contentView");
					contentView.setBackGroundColor(contentViewObj
							.getString("backGroundColor"));
				} catch (JSONException e) {

				}

				mocoopStyle.setTopBar(topBar);
				mocoopStyle.setContentView(contentView);

				styleMap.put(path, mocoopStyle);

				return mocoopStyle;
			} catch (JSONException e) {
				e.printStackTrace();
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean execute(final String action, final CordovaArgs args,
			final CallbackContext callbackContext) throws JSONException {

		if ("show".equals(action)) {
			getActivity().showMeRightNow();
		} else if ("jumpNewPage".equals(action)) {
			JSONObject json = null;
			PageInfoEntity pageInfoEntity = new PageInfoEntity();
			try {
				json = args.getJSONObject(0);
				pageInfoEntity.setAnimated(true);
				pageInfoEntity.setTitle(json.getString("topBarTitle"));
				pageInfoEntity.setUrl(json.getString("URL"));
				pageInfoEntity.setConfigFileSubPath(json
						.getString("UIFeatureJSONFileSubPath"));
				pageInfoEntity.setMocoopStyle(getStyleObject(
						pageInfoEntity.getConfigFileSubPath(), getActivity()));
			} catch (JSONException e) {
				e.printStackTrace();
			}

			getActivity().jumpNewPage(pageInfoEntity);
		} else if ("goback".equals(action)) {
			getActivity().goBack(null);
		} else if ("backtotarget".equals(action)) {

		} else if ("killLaunch".equals(action)) {
			((BaseActivity) cordova.getActivity()).getJFQCActivity()
					.hideLauchView();
		} else {

		}

		return true;
	}
}
